package main;

import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

import configuration.Configuration;

import utils.Contents;
import utils.MessageBox;

public class Matrix_Change extends JFrame{
	


	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private int imgCount=1;
	private JPanel panel ;
	/**
	 * Launch the application
	 * @param args
	 */

	/**
	 * Create the application
	 * @return 
	 */
	private  Matrix_Change(int count) {
		if(count>1)
			this.imgCount = count;
		createContents();
	}
	private static Matrix_Change instance;
	public static Matrix_Change getInstance( int count){
		if(instance==null){
			instance = new Matrix_Change(count);
		}
		return instance;
	}
	/**
	 * Initialize the contents of the frame
	 */
	private void createContents() {
		setTitle("图片展示");
		ImageIcon icon=new ImageIcon(Contents.ICON_PATH);//
		setIconImage(icon.getImage());
		getContentPane().setLayout(null);
		setResizable(false);
		setBounds(100, 100, 985, 750);
		setDefaultCloseOperation(DISPOSE_ON_CLOSE); 

	   
		//下一张 
		final JButton button = new JButton();
		button.setFont(new Font("宋体", Font.PLAIN, 14));
		button.setText("下一张");
		button.setBounds(770, 633, 90, 42);
		button.setOpaque(false);
		getContentPane().add(button);
		//上一张
		final JButton button_1 = new JButton();
		button_1.setFont(new Font("宋体", Font.PLAIN, 14));
		button_1.setText("上一张");
		button_1.setBounds(668, 633, 90, 42);
		button_1.setOpaque(false);
		getContentPane().add(button_1);
		//重新开始
		final JButton button_0 = new JButton();
		button_0.setFont(new Font("宋体", Font.PLAIN, 14));
		button_0.setText("重新开始");
		button_0.setBounds(870, 633, 90, 42);
		getContentPane().add(button_0);
		//panel
		 panel = new GetPanel(960,680,System.getProperty("user.dir")+Contents.IMG_Matrix_Change_PATH+imgCount+".JPG");
			panel.setBounds(10, 10, 960, 720);
			getContentPane().add(panel);
		//监听
			addWindowListener(new WindowAdapter() {
				public void windowClosing(WindowEvent e){
					new Configuration(Contents.CONFIG_PATH).setValue("imgCount_Matrix", imgCount);
					this.windowClosed(e);
				}
			});
		button_0.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent   arg0){
				if(MessageBox.Show("你确定重新开始吗？", 12)==0){
					imgCount=2;
					changeBgImg(true);
				}
			}
		});
		button_1.addActionListener(new ActionListener(){
			public   void   actionPerformed(ActionEvent   arg0) {
				changeBgImg(true);
			}
		});
		button.addActionListener(new ActionListener(){
			public   void   actionPerformed(ActionEvent   arg0) {
				changeBgImg(false);
			}
		});
		panel.addMouseListener(mouse);
		
	}
	private MouseListener mouse = new MouseListener() {

		@Override
		public void mouseClicked(MouseEvent arg0) {
			int x = 2*arg0.getX();
			changeBgImg(x<=panel.getWidth());
		}
		@Override
		public void mouseEntered(MouseEvent e) {
		}
		@Override
		public void mouseExited(MouseEvent e) {
		}
		@Override
		public void mousePressed(MouseEvent e) {
		}
		@Override
		public void mouseReleased(MouseEvent e) {
		}
		
	};

	/**
	 * function changeBgImg(Boolean previous)
	 * 改变图片。当条件为真的时候往前翻页，假就向后翻页
	 * 
	 * */
	private  void  changeBgImg(Boolean previous){
		if(previous){
			if(imgCount==1){
				MessageBox.Show("已是第一章图片!");
			}else{
				imgCount--;
			}
		}else {
			if(imgCount==Contents.IMG_Matrix_Change_COUNT){
				MessageBox.Show("已是最后一章图片!!");
			}else {
				imgCount++;
			}
		}
		getContentPane().remove(panel);
		panel = new GetPanel(960,680,System.getProperty("user.dir")+Contents.IMG_Matrix_Change_PATH+imgCount+".JPG");
		panel.setBounds(10, 10, 960, 720);
		panel.addMouseListener(mouse);
//		panel.addKeyListener(key);
		getContentPane().add(panel);
		repaint();
	}

}
